package com.sc.study.clazz.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import com.sc.study.clazz.domain.ScClazzSubjectTeacher;
import com.sc.study.clazz.mapper.ScClazzMapper;
import com.sc.study.clazz.mapper.ScClazzSubjectTeacherMapper;
import com.sc.study.foundation.mapper.ScSubjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sc.study.clazz.mapper.ScClazzSubjectMapper;
import com.sc.study.clazz.domain.ScClazzSubject;
import com.sc.study.clazz.service.IScClazzSubjectService;

/**
 * 班级科目Service业务层处理
 *
 * @author sc
 * @date 2022-02-27
 */
@Service
public class ScClazzSubjectServiceImpl implements IScClazzSubjectService {
    @Autowired
    private ScClazzSubjectMapper scClazzSubjectMapper;
    @Autowired
    private ScSubjectMapper scSubjectMapper;
    @Autowired
    private ScClazzMapper scClazzMapper;
    @Autowired
    private ScClazzSubjectTeacherMapper scClazzSubjectTeacherMapper;

    /**
     * 查询班级科目
     *
     * @param id 班级科目主键
     * @return 班级科目
     */
    @Override
    public ScClazzSubject selectScClazzSubjectById(Long id) {
        return scClazzSubjectMapper.selectScClazzSubjectById(id);
    }

    /**
     * 查询班级科目列表包含科目、班级
     *
     * @param scClazzSubject 班级科目
     * @return 班级科目
     */
    @Override
    public List<ScClazzSubject> selectScClazzSubjectList(ScClazzSubject scClazzSubject) {
        return scClazzSubjectMapper.selectClazzSubjectIncludeSubjectAndClazz(scClazzSubject);
    }

    /**
     * 新增班级科目
     *
     * @param scClazzSubject 班级科目
     * @return 结果
     */
    @Override
    public int insertScClazzSubject(ScClazzSubject scClazzSubject) {
        ScClazzSubject scClazzSubject1=new ScClazzSubject();
        scClazzSubject1.setSubid(scClazzSubject.getSubid());
        scClazzSubject1.setClazzid(scClazzSubject.getClazzid());
        scClazzSubject1.setSuborder(scClazzSubject.getSuborder());
        List<ScClazzSubject> scClazzSubjectList = scClazzSubjectMapper.selectScClazzSubjectList(scClazzSubject1);
        if(scClazzSubjectList.size()>0){
            return 0;
        }else{
            return scClazzSubjectMapper.insertScClazzSubject(scClazzSubject);
        }


    }

    /**
     * 修改班级科目
     *
     * @param scClazzSubject 班级科目
     * @return 结果
     */
    @Override
    public int updateScClazzSubject(ScClazzSubject scClazzSubject) {
        return scClazzSubjectMapper.updateScClazzSubject(scClazzSubject);
    }

    /**
     * 批量删除班级科目和班级科目授课老师
     *
     * @param ids 需要删除的班级科目主键
     * @return 结果
     */
    @Override
    public int deleteScClazzSubjectByIds(Long[] ids) {
        Arrays.stream(ids).forEach(id->{
            // 根据id找到对应的班级科目
            ScClazzSubject scClazzSubject = scClazzSubjectMapper.selectScClazzSubjectById(id);

            // 根据班级科目找到对应的班级授课老师进行删除
            ScClazzSubjectTeacher scClazzSubjectTeacher=new ScClazzSubjectTeacher();
            scClazzSubjectTeacher.setClazzsubid(scClazzSubject.getId());
            scClazzSubjectTeacherMapper.deleteScClazzSubjectTeacher(scClazzSubjectTeacher);
        });
        return scClazzSubjectMapper.deleteScClazzSubjectByIds(ids);
    }

    /**
     * 删除班级科目信息
     *
     * @param id 班级科目主键
     * @return 结果
     */
    @Override
    public int deleteScClazzSubjectById(Long id) {
        return scClazzSubjectMapper.deleteScClazzSubjectById(id);
    }

    /**
     * 根据条件删除
     *
     * @param scClazzSubject 班级科目
     * @return 结果
     */
    @Override
    public int deleteScClazzSubject(ScClazzSubject scClazzSubject) {
        return scClazzSubjectMapper.deleteScClazzSubject(scClazzSubject);
    }


}
